﻿using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Windows;

namespace 工厂方法模式
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //原有方法每个工厂都要实例化代码较多

            ////红旗工厂生产红旗车
            //var hongQiFactory=new HongQiFactory();
            //var car=  hongQiFactory.CreateCar();
            //Debug.WriteLine(car.ProductData());

            ////Bwn工厂生产Bwn车
            //var bwnFactory=new BwnFactory();
            //var car1 = bwnFactory.CreateCar();
            //Debug.WriteLine(car1.ProductData());

            //现有方法
            //App.config添加appSettings 利用Assembly反射 新建实例HongQiFactory
            var factoryType = ConfigurationManager.AppSettings["No" + 1];
            var factory = (CarFactory)Assembly.Load("工厂方法模式").CreateInstance("工厂方法模式."+factoryType);
            var car= factory?.CreateCar();
            if (car != null)
                Debug.WriteLine(car.ProductType());
        }
    }
}
